package com.feilx.changgou.pay.mq;

import com.changgou.mq.MQConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderMQConfig {


    // rabbitmq 消息序列化机制 json转换
    @Bean
    public MessageConverter messageConverter(){
        return  new Jackson2JsonMessageConverter();
    }

    // 定义队列
    @Bean(MQConstant.ORDER_QUEUE)
    public Queue queue(){
        return  new Queue(MQConstant.ORDER_QUEUE,true,false,false);
    }

    // 定义交换机
    @Bean(MQConstant.ORDER_EXCHANGE)
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(MQConstant.ORDER_EXCHANGE).durable(true).build();
    }

    // 绑定队列交换机
    @Bean
    public Binding binding(@Qualifier(MQConstant.ORDER_EXCHANGE) Exchange exchange,
                           @Qualifier(MQConstant.ORDER_QUEUE)Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(MQConstant.ORDER_ROUTINGKEY).noargs();
    }

}
